package com.bridgeintelligent.tag.customer.looklike;

import com.bridgeintelligent.tag.customer.service.IIssueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.web.bind.annotation.*;

@Api(tags = "相似客群下发服务接口")
@AllArgsConstructor
@RestController
@RequestMapping("/api/lookLikeIssue")
@Slf4j
public class LookLikeIssueController {

    private ILookLikeIssueService lookLikeIssueService;

    private IIssueService issueService;

    @ApiOperation("lookLike新增导出下发")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "issue", value = "相似客群下发信息")
    })
    @PostMapping("/addIssue")
    public LookLikeResp addIssue(@RequestBody LookLikeIssue issue) {
        try {
            if(!NumberUtils.isDigits(issue.getCcis())){
                return LookLikeResp.error("CCIS工单必须为数字");
            }
            int count = issueService.countById(issue.getIssueKey());
            if(count>0){
                return LookLikeResp.error("下发id不能重复！");
            }
            count = issueService.countByName(issue.getIssueName());
            if(count>0){
                return LookLikeResp.error("下发名称重复，请修改下发名称后重新提交！");
            }
            lookLikeIssueService.addLookLikeIssue(issue);
            return LookLikeResp.ok();
        } catch (Exception e) {
            log.error("lookLike新增导出下发信息失败：{}:",e);
            return LookLikeResp.error("lookLike新增导出下发信息失败");
        }
    }

    @ApiOperation("lookLike修改导出下发")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "issue", value = "相似客群下发信息")
    })
    @PostMapping("/updateIssue")
    public LookLikeResp updateIssue(@RequestBody LookLikeIssue issue) {
        try {
            if(StringUtils.isBlank(issue.getChannelId())){
                return LookLikeResp.error("下发id不能为空！");
            }
            int count = issueService.countByNameAndNotId(issue.getIssueKey(),issue.getIssueName());
            if(count>0){
                return LookLikeResp.error("下发名称重复，请修改下发名称后重新提交！");
            }
            if(!NumberUtils.isDigits(issue.getCcis())){
                return LookLikeResp.error("ccis工单名称必须为数字！");
            }
            //先校验是否有任务在运行
            boolean run = lookLikeIssueService.isRun(issue.getId());
            if (run){
                return LookLikeResp.error("当前下发任务有正在运行的任务！！");
            }
            lookLikeIssueService.updateLookLikeIssue(issue);
            return LookLikeResp.ok();
        } catch (Exception e) {
            log.error("lookLike修改导出下发信息失败：{}:",e);
            return LookLikeResp.error("lookLike修改导出下发信息失败");
        }
    }

    @ApiOperation("lookLike修改任务管理下发")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "issue", value = "相似客群任务管理下发信息")
    })
    @PostMapping("/updateIssueTask")
    public LookLikeResp updateIssueTask(@RequestBody LookLikeIssue issue) {
        try {
            if(StringUtils.isBlank(issue.getChannelId())){
                return LookLikeResp.error("下发id不能为空！");
            }
            int count = issueService.countByNameAndNotId(issue.getIssueKey(),issue.getIssueName());
            if(count>0){
                return LookLikeResp.error("下发名称重复，请修改下发名称后重新提交！");
            }
            if(!NumberUtils.isDigits(issue.getCcis())){
                return LookLikeResp.error("ccis工单名称必须为数字！");
            }
            //先校验是否有任务在运行
            boolean run = lookLikeIssueService.isRun(issue.getId());
            if (run){
                return LookLikeResp.error("当前下发任务有正在运行的任务！！");
            }
            lookLikeIssueService.updateLookLikeIssueTask(issue);
            return LookLikeResp.ok();
        } catch (Exception e) {
            log.error("lookLike任务管理下发信息失败：{}:",e);
            return LookLikeResp.error("lookLike任务管理下发信息失败");
        }
    }

   @ApiOperation("查看任务下是否有任务在执行true有false没有")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "taskId", value = "任务ID")
    })
    @GetMapping("/isRun")
    public LookLikeResp isRun(String taskId) {
        try {
            //先校验是否有任务在运行
            boolean data = lookLikeIssueService.isRun(taskId);
            return LookLikeResp.ok(data);
        }catch (Exception e){
            log.error("lookLike查看任务下是否有任务在执行失败：{}:",e);
            return LookLikeResp.error("lookLike查看任务下是否有任务在执行失败");
        }
    }

    @ApiOperation("查看Issue详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "下发ID")
    })
    @GetMapping("/findLookIssue")
    public LookLikeResp findLookIssue(String id) {
        try {
            LookLikeIssue data = lookLikeIssueService.findLookIssue(id);
            return LookLikeResp.ok(data);
        }catch (Exception e){
            log.error("lookLike查看Issue详情失败：{}:",e);
            return LookLikeResp.error("lookLike查看Issue详情失败");
        }
    }

    @ApiOperation("查看IssueTask详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "taskId")
    })
    @GetMapping("/findLookIssueTask")
    public LookLikeResp findLookIssueTask(String id) {
        try {
            //先校验是否有任务在运行
            LookLikeIssue data = lookLikeIssueService.findLookIssueTask(id);
            return LookLikeResp.ok(data);
        }catch (Exception e){
            log.error("lookLike查看IssueTask详情失败：{}:",e);
            return LookLikeResp.error("lookLike查看IssueTask详情失败");
        }
    }

    @ApiOperation("looklike导出下发重试")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "logId", value = "任务ID")
    })
    @GetMapping("/isTry")
    public LookLikeResp isTry(String logId) {
        try {
            lookLikeIssueService.istry(logId);
            return LookLikeResp.ok();
        }catch (Exception e){
            log.error("looklike导出下发重试失败：{}:",e);
            return LookLikeResp.error("looklike导出下发重试失败");
        }
    }

}